package com.dhcy.mqtt.service.impl;


import com.dhcy.mqtt.par.ht.*;
import com.dhcy.mqtt.result.ht.TableResultData;
import com.dhcy.mqtt.util.AppResultData;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcy.mqtt.dao.SysResourceMapper;
import com.dhcy.mqtt.model.SysResource;
import com.dhcy.mqtt.service.SysResourceService;
import java.util.ArrayList;

import com.dhcy.mqtt.util.DateUtil;
import com.dhcy.mqtt.util.IsNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import com.dhcy.mqtt.result.ht.SysResourceResult;

@Service
public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysResource> implements SysResourceService{
@Autowired
    SysResourceMapper sysResourceMapper;

    @Override
    public TableResultData sysResourceList(SysResourceListPar sysResourceListPar) {
        IPage<SysResource> page = new Page<>(sysResourceListPar.getPage(), sysResourceListPar.getPageSize());
        QueryWrapper<SysResource> queryWrapper = new QueryWrapper<>();

        if(!IsNull.isNullOrEmpty(sysResourceListPar.getResuorceName())){
            queryWrapper.like("resuorceName",sysResourceListPar.getResuorceName());
        }
        if(!IsNull.isNullOrEmpty(sysResourceListPar.getLinkUrl())){
            queryWrapper.like("linkUrl",sysResourceListPar.getLinkUrl());
        }
        if(!IsNull.isNullOrEmpty(sysResourceListPar.getResourceType())){
            queryWrapper.like("resourceType",sysResourceListPar.getResourceType());
        }
        if(!IsNull.isNullOrEmpty(sysResourceListPar.getResourceCode())){
            queryWrapper.like("resourceCode",sysResourceListPar.getResourceCode());
        }
        if(!IsNull.isNullOrEmpty(sysResourceListPar.getParentId())){
            queryWrapper.like("parentId",sysResourceListPar.getParentId());
        }
        if(!IsNull.isNullOrEmpty(sysResourceListPar.getDelFlg())){
            queryWrapper.like("delFlg",sysResourceListPar.getDelFlg());
        }
        if(!IsNull.isNullOrEmpty(sysResourceListPar.getCREATER())){
            queryWrapper.like("CREATER",sysResourceListPar.getCREATER());
        }
        if(!IsNull.isNullOrEmpty(sysResourceListPar.getCreateTime())){
            queryWrapper.like("createTime",sysResourceListPar.getCreateTime());
        }
        if(!IsNull.isNullOrEmpty(sysResourceListPar.getUPDATOR())){
            queryWrapper.like("UPDATOR",sysResourceListPar.getUPDATOR());
        }
        if(!IsNull.isNullOrEmpty(sysResourceListPar.getSort())){
            queryWrapper.eq("sort",sysResourceListPar.getSort());
        }
        if(!IsNull.isNullOrEmpty(sysResourceListPar.getIcon())){
            queryWrapper.like("icon",sysResourceListPar.getIcon());
        }
        page = sysResourceMapper.selectPage(page, queryWrapper);
        List<SysResourceResult> resultList = new ArrayList<>();
        for (SysResource sysResource : page.getRecords()) {
            SysResourceResult sysResourceResult = new SysResourceResult();
            BeanUtils.copyProperties(sysResource, sysResourceResult);
            resultList.add(sysResourceResult);
        }
        return TableResultData.success(page.getTotal(), resultList);
    }

    @Override
    public AppResultData sysResourceAdd(SysResourceAddPar sysResourceaddPar) {
        SysResource sysResource = new SysResource();
        BeanUtils.copyProperties(sysResourceaddPar, sysResource);
        if (IsNull.isNullOrEmpty(sysResource.getId())) {
            sysResource.setCreateTime(DateUtil.timeStamp());
            sysResourceMapper.insert(sysResource);
        } else {
            sysResource.setUpdateTime(new Date());
            sysResourceMapper.updateById(sysResource);
        }
        return AppResultData.successMessage("成功");
    }

    @Override
    public AppResultData sysResourceDel(Integer id) {
        int count = sysResourceMapper.deleteById(id);
        if (count > 0)
            return AppResultData.successMessage("成功");
        else {
            return AppResultData.errorMessage("当前记录不存在");
        }
    }}
